package com.baizhi.controller;


import com.baizhi.entity.LayuiEms;
import com.baizhi.service.LayuiEmsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhengg
 * @since 2020-07-29
 */
@RestController
@RequestMapping("/layui-ems")
public class LayuiEmsController {
    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;
    @Autowired
    private LayuiEmsService es;
    @RequestMapping("selectAll")
    public Map<String,Object> selectAll(@RequestParam(required = false) Integer deptid){
        List<LayuiEms> emsList= es.selectEmsAndDept(deptid);
        emsList.forEach(System.out::println);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",emsList);
        return map;
    }
    @RequestMapping("/uploadHeadImg")
    public Map<String,Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path,file.getOriginalFilename()));
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg",file.getOriginalFilename());
        return map;
    }
    @RequiresPermissions("ems:delete")
    @RequestMapping("/add")
    public Map<String,Object> add(LayuiEms ems){
        System.out.println(ems.getPicPath());
        es.save(ems);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        return map;
    }
}
